package sy.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sy.jackson.SysProjectMixin;
import sy.jackson.AppEntryLogMixin;
import sy.model.SysDivision;
import sy.model.SysProject;
import sy.model.WYAccount;
import sy.service.WYAccountServiceI;
import sy.service.WYBaseServiceI;
import sy.util.JGpush;
import sy.util.ResultJson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("v1/wyBase")
public class WYBaseController {
	@Autowired
	WYBaseServiceI wyBaseService;
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	/**
	 * 
	 * @Title: login
	 * @Description: TODO
	 * @param: @param telephone
	 * @param: @param password
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月6日 下午3:47:21
	 * @throws
	 */
	@RequestMapping(value = "/login/{workCode}/{password}", method = RequestMethod.GET,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String login(@PathVariable("workCode") String workCode, @PathVariable("password") String password){
		ResultJson resultJson = wyBaseService.login(workCode, password);
		objectMapper.addMixInAnnotations(WYAccount.class, AppEntryLogMixin.BasicInfo.class);
		objectMapper.addMixInAnnotations(SysDivision.class, SysProjectMixin.BasicInfo.class);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
	}
	
	/**
	 * 
	 * @Title: logout
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月6日 下午3:47:28
	 * @throws
	 */
	@RequestMapping(value = "/logout/{token}", method = RequestMethod.GET)
	@ResponseBody
	public ResultJson logout(@PathVariable("token") String token){
		ResultJson resultJson = wyBaseService.logout(token);
		return resultJson;	
	}
	
	/**
	 * 
	 * @Title: getVerificatCode
	 * @Description: TODO
	 * @param: @param telephone
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月8日 下午7:44:48
	 * @throws
	 */
	@RequestMapping(value="/getVerificatCode/{telephone}/{workCode}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getVerificatCode(@PathVariable("telephone") String telephone,@PathVariable("workCode") String workCode){
		/**
		 * 1.查找该手机是否已注册过；
		 * 2.没有注册，则先生成一个6位数的验证码,写入AppAccountVerificatCode表，再调用短信发送接口发送到传过来的手机
		 */
		ResultJson resultJson = wyBaseService.getVerificatCode(telephone,workCode);
		return resultJson;
	}
	/**
	 * 
	 * @Title: testVerificatCode
	 * @Description: TODO
	 * @param: @param telephone
	 * @param: @param verificatCode
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月8日 下午7:44:53
	 * @throws
	 */
	@RequestMapping(value="/testVerificatCode", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson testVerificatCode(@RequestParam("workCode") String workCode,@RequestParam("telephone") String telephone, @RequestParam("verificatCode") String verificatCode){
		ResultJson resultJson = wyBaseService.testVerificatCode(workCode, telephone, verificatCode);
		return resultJson;
	}
	/**
	 * 
	 * @Title: resetPassword
	 * @Description: TODO
	 * @param: @param telephone
	 * @param: @param password
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月9日 上午11:14:39
	 * @throws
	 */
	@RequestMapping(value="/password/reset", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson resetPassword( @RequestParam("token")String token,  @RequestParam("password")String password){
		ResultJson resultJson = wyBaseService.resetPassword(token, password);
		return resultJson;
	}
	/**
	 * 
	 * @Title: resetPassword
	 * @Description: TODO
	 * @param: @param workCode
	 * @param: @param password
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月13日 下午3:24:08
	 * @throws
	 */
	@RequestMapping(value="/password/modify", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson modifyPassword( @RequestParam("workCode")String workCode,  @RequestParam("password")String password){
		ResultJson resultJson = wyBaseService.modifyPassword(workCode, password);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title: getWYUser
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月14日 下午6:49:57
	 * @throws
	 */
	@RequestMapping(value="/wyUser/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getWYUser( @PathVariable("token")String token){
		ResultJson resultJson = wyBaseService.getWYUserInfo(token);
		return resultJson;
	}
	/**
	 * 获取当前安防事件未处理总数，停车场异常抬杆总数，设备告警总数
	 * @Title: getTipsCount
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月28日 上午10:39:12
	 * @throws
	 */
	@RequestMapping(value="/tips/count/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getTipsCount( @PathVariable("token")String token){
		ResultJson resultJson = wyBaseService.getTipsCount(token);
		return resultJson;
	}
	
}
